package com.xbongbong.paas.script.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.marketManage.pojo.dto.MarketActivity4StatisticsRecalculateDTO;
import com.xbongbong.pro.marketManage.pojo.dto.MarketActivity4StatisticsSyncContractDTO;
import com.xbongbong.pro.marketManage.pojo.dto.MarketActivityForStaticDTO;
import com.xbongbong.saas.service.MarketActivityForStaticService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @author guohui.zhang
 * @Title:
 * @date 2021/1/315:41
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/script/marketActivity/static")
public class MarketAcitvityStaticController {
    private static final Logger LOG = LoggerFactory.getLogger(MarketAcitvityStaticController.class);
    @Resource
    private MarketActivityForStaticService marketActivityForStaticService;

    /**
     * 此脚本已废弃 新增统计到合同后不可用
     * date: 2022/3/16 9:22 上午
     *
     * @param marketActivityForStaticDTO:
     * @param br:
     * @author yingjf
     */
    @Deprecated
    @RequestMapping(value = "/init", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String init(@RequestBody @Valid MarketActivityForStaticDTO marketActivityForStaticDTO, BindingResult br) throws Exception {
        XbbResponse<BaseVO> response;
        if (!br.hasErrors()) {
            // BaseVO baseVO = marketActivityForStaticService.staticMarketActivity(marketActivityForStaticDTO);
            try {
                // TODO: 2022/3/17 此脚本已废弃  已被下面的recalculate替换
                BaseVO baseVO = marketActivityForStaticService.staticMarketActivity(marketActivityForStaticDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        } else {
            LOG.warn("-----------/script/marketActivity/static is over");
        }
        return JSON.toJSONString("{\"msg\":\"success\"}");
    }

    /**
     * 刷合同脚本   支持某一时间点的前后
     * date: 2022/3/16 9:10 上午
     *
     * @author yingjf
     * xbbcode 123456
     */
    @RequestMapping(value = "/syncContract", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String syncContract(@RequestBody @Valid MarketActivity4StatisticsSyncContractDTO marketActivity4StatisticsSyncContractDTO, BindingResult br) throws Exception {
        XbbResponse<BaseVO> response;
        if (!br.hasErrors()) {
            try {
                BaseVO baseVO = marketActivityForStaticService.syncContract(marketActivity4StatisticsSyncContractDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        } else {
            LOG.warn("-----------/script/marketActivity/syncContract is over");
        }
        return JSON.toJSONString("{\"msg\":\"success\"}");
    }

    /**
     * 重新统计脚本  支持某一市场活动或整个公司(不包括回收站)
     * date: 2022/3/16 9:11 上午
     *
     * @author yingjf
     * xbbcode 123456
     */
    @Deprecated
    @RequestMapping(value = "/recalculate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String recalculate(@RequestBody @Valid MarketActivity4StatisticsRecalculateDTO marketActivity4StatisticsRecalculateDTO, BindingResult br) throws Exception {
        XbbResponse<BaseVO> response;
        if (!br.hasErrors()) {
            try {
                BaseVO baseVO = marketActivityForStaticService.recalculate(marketActivity4StatisticsRecalculateDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        } else {
            LOG.warn("-----------/script/marketActivity/recalculate is over");
        }
        return JSON.toJSONString("{\"msg\":\"success\"}");
    }

    /**
     * 重新统计脚本  支持某一市场活动或整个公司(包括回收站)
     * date: 2022/3/23 10:40 上午
     * @author yingjf
     * @param marketActivity4StatisticsRecalculateDTO:
     * @param br:
     */
    @RequestMapping(value = "/recalculateIncludeRecycle", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String recalculateIncludeRecycle(@RequestBody @Valid MarketActivity4StatisticsRecalculateDTO marketActivity4StatisticsRecalculateDTO, BindingResult br) throws Exception {
        XbbResponse<BaseVO> response;
        if (!br.hasErrors()) {
            try {
                BaseVO baseVO = marketActivityForStaticService.recalculateIncludeRecycle(marketActivity4StatisticsRecalculateDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        } else {
            LOG.warn("-----------/script/marketActivity/recalculate is over");
        }
        return JSON.toJSONString("{\"msg\":\"success\"}");
    }


}
